var vue = new Vue({
  el: '#app',
  data: {
    multipleSelection: [],
    title: '',
    days: '',
    record: '',
    demandData: [],
    meetingData: [],
    spanArr: [],
    pos: 0,
    totalSize: 0,
    pageIndex: 1,
    pageSize: 10,
    totalSize1: 0,
    pageIndex1: 1,
    pageSize1: 10,
    directorOptions: [{
      label: '主任1',
      value: '1'
    }, {
      label: '主任2',
      value: '2'
    }],
    companyOptions: [{
      label: '辽宁1',
      value: '1'
    }, {
      label: '辽宁2',
      value: '2'
    }, {
      label: '辽宁3',
      value: '3'
    }, {
      label: '辽宁4',
      value: '4'
    }, {
      label: '辽宁5',
      value: '5'
    }, {
      label: '辽宁6',
      value: '6'
    }, {
      label: '辽宁7',
      value: '7'
    }],
    pickerOptions1: {
      disabledDate(time) {
        return time.getTime() > Date.now()
      }
    },
    // 添加界面表单
    form: {

      id: '',
      date_meeting: '',
      enddate_demand: '',
      company: [],
      content: ''
    },
    // 表单验证规则
    rules: {
      date_meeting: [{
        required: true,
        message: '请选择周例会',
        trigger: 'change'
      }],
      enddate_demand: [{
        type: 'date',
        required: true,
        message: '请选择时间',
        trigger: 'blur'
      }],
      company: [{
        type: 'array',
        required: true,
        message: '选择参会单位',
        trigger: 'change'
      }],
      content: [{
        required: true,
        message: '请输入要求内容',
        trigger: 'blur'
      }]
    },

    formLabelWidth: '120px',
    // 对话框显示状态
    addVisible: false,
    inVisible: false

  },
  created: function() {
    this.getDic()
    this.initGrid()
    this.initGrid1()
  },
  mounted() {

  },

  methods: {
    // 添加记录
    add: function(id) {
      this.addVisible = true
    },

    // 删除
    deleteMeeting: function(rid) {
      if (this.multipleSelection.length === 0) {
        this.$message({
          showClose: true,
          message: '请至少选择一条数据',
          type: 'warning'
        })
        return false
      }
      var ids = this.multipleSelection.map(item => item.id).join()
      if (confirm('确定删除吗')) {
        var that = this
        $.post('weekMeeting/removeForBatch', {
          ids: ids
        }, function(result) {
          if (result.data) {
            that.$message({
              message: '删除成功',
              type: 'success'
            })
            that.initGrid()
          } else {
            that.$message({
              message: '删除失败',
              type: 'warning'
            })
          }
        })
      }
    },
    // 获取字典下拉菜单
    getDic: function() {
      var that = this
      $.get('weekMeeting/getDic', function(result) {
        that.directorOptions = result.data.user
        that.companyOptions = result.data.org
      })
    },
    // 初始化表格
    initGrid: function() {
      var that = this
      $.post('specialitydemand/listByPageByDate', {
        index: that.pageIndex,
        size: that.pageSize
      }, function(result) {
        console.log(result)
        that.totalSize = result.data.total
        that.demandData = result.data.data
        that.getSpanArr(that.demandData)
      })
    },
    // 获取周会议
    initGrid1: function() {
      var that = this
      $.post('weekMeeting/listByPageByDate', {
        index: that.pageIndex1,
        size: that.pageSize1
      }, function(result) {
        that.totalSize1 = result.data.total
        that.meetingData = result.data.data
      })
    },
    getSpanArr(data) {
      for (var i = 0; i < data.length; i++) {
        if (i === 0) {
          this.spanArr.push(1)
          this.pos = 0
        } else { // 判断当前元素与上一个元素是否相同
          if (data[i]['id_main'] === data[i - 1]['id_main']) {
            this.spanArr[this.pos] += 1
            this.spanArr.push(0)
          } else {
            this.spanArr.push(1)
            this.pos = i
          }
        }
      }
    },
    arraySpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex <= 5 && columnIndex > 1) {
        const _row = this.spanArr[rowIndex]
        const _col = _row > 0 ? 1 : 0
        return {
          rowspan: _row,
          colspan: _col
        }
      }
    },
    // 翻页
    handleSizeChange(val) {
      this.pageSize = val
      this.initGrid()
    },
    // 改变每页显示条数
    handleCurrentChange(val) {
      this.pageIndex = val
      this.initGrid()
    },
    // 内部插入表格每页几条
    handleSizeChange1(val) {
      console.log(`每页 ${val} 条`)
      this.pageSize1 = val
      this.initGrid1()
    },
    // 内部插入表格翻页
    handleCurrentChange1(val) {
      console.log(`当前页: ${val}`)
      this.pageIndex1 = val
      this.initGrid1()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 添加会议
    addMeeting: function() {
      function S4() {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)
      }

      var id_main = (S4() + S4() + '-' + S4() + '-' + S4() + '-' + S4() + '-' + S4() + S4() + S4())
      var that = this
      var originator = document.getElementById('userName').value
      var enddate_demandStr = that.form.enddate_demand.format('yyyy-MM-dd')
      var arr = []
      for (var c of this.form.company) {
        var info = {}
        info.id_main = id_main
        info.id_meeting = this.form.id
        info.originator = originator
        info.enddate_demand = enddate_demandStr
        info.company_demand = c.value
        info.name_company_demand = c.label
        info.content = this.form.content
        arr.push(info)
      }
      console.log(arr)
      $.ajax({
        url: 'specialitydemand/saveAll',
        type: 'POST',
        data: JSON.stringify(arr),
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function(result) {
          if (result.data) {
            that.$message({
              message: '增加成功',
              type: 'success'
            })
            that.addVisible = false
            that.initGrid()
          } else {
            that.$message({
              message: '增加失败',
              type: 'warning'
            })
          }
        }
      })
    },
    // 关闭dialog前执行的方法
    handleClose(done) {
      var that = this
      this.$confirm('确认关闭？')
        .then(_ => {
          if (that.addVisible) {
            that.addVisible = false
            this.$refs['form'].resetFields()
          }
        })
        .catch(_ => {
        })
    },
    handleClose1(done) {
      var that = this
      that.inVisible = false
    },
    // 注入记录
    init(row) {
      this.form.date_meeting = row.date_meeting.toString()
      this.form.id = row.id
      this.inVisible = false
    },
    // 添加表单验证
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.addMeeting()
        } else {
          return false
        }
      })
    },
    sure(row) {
      console.log(row)
    }
  }
})
